<template>
<XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem :key="category.top.id" v-if="category.top" :to="`/category/${category.top.id}`">{{category.top.name}}</XtxBreadItem>
    <transition name="fade-right" mode="out-in">
        <XtxBreadItem :key="category.sub.id" v-if="category.sub">{{category.sub.name}}</XtxBreadItem>
    </transition>
</XtxBread>
</template>

<script>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
export default {
    name: 'SubBread',
    setup () {
        // 面包屑数据，顶级类目，二级类目
        const store = useStore ()
        const route = useRoute ()
        const category = computed (() => {
            const cate = {}
            store.state.category.list.forEach(top => {
                if(top.children) {
                    const sub = top.children.find(sub => sub.id === route.params.id)
                    if(sub) {
                        cate.top = {id: top.id, name: top.name}
                        cate.sub = {id: sub.id, name: sub.name}
                    }
                }
            })
            return cate
        })
        return {category}
    }
   
    

}
</script>

<style>

</style>